package bank;


import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration// 代替spring.xml文件，在这个类中完成配置类
@ComponentScan("bank")//组件扫描
@EnableTransactionManagement // 代替spring.xml文件中的<tx:annotation-driven/>
public class spring6Config {
    @Bean(name="dataSource")
    public DruidDataSource getDruidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/spring6");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");

        return druidDataSource;
    }
    @Bean(name="txManager")
    public JdbcTransactionManager getJdbcTransactionManager(DataSource dataSource){
        JdbcTransactionManager txManager = new JdbcTransactionManager();
        txManager.setDataSource(dataSource);
        return txManager;
    }
    @Bean(name="jdbcTemplate")
    public JdbcTemplate getJdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

}
